<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="Mosaddek">
    <meta name="keyword" content="FlatLab, Dashboard, Bootstrap, Admin, Template, Theme, Responsive, Fluid, Retina">

    <link rel="shortcut icon" href="img/favicon.html">

    <title>权限分配</title>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/bootstrap-reset.css" rel="stylesheet">
    <!--external css-->
    <link href="assets/font-awesome/css/font-awesome.css" rel="stylesheet"/>
    <!-- Custom styles for this template -->
    <link href="css/style.css" rel="stylesheet">
    <link href="css/style-responsive.css" rel="stylesheet"/>
    <link rel="stylesheet" href="localjs/layui/css/layui.css">
    <script src="localjs/layui/layui.js" charset="utf-8"></script>
    <!-- HTML5 shim and Respond.js IE8 support of HTML5 tooltipss and media queries -->
    <!--[if lt IE 9]>
    <script src="js/html5shiv.js"></script>
    <script src="js/respond.min.js"></script>
    <![endif]-->
</head>

<body>

<section id="container" class="">
    <!--header start-->
    <div id="header">

    </div>
    <!--main content start-->
    <section id="main-content">
        <section class="wrapper">
            <!-- page start-->


            <div class="col-lg-12">
                <section class="panel">
                    <header class="panel-heading">
                        <p class="text-center invoice-btn">
                            <span style="font-size: 24px;">功能分配</span>
                        </p>

                    </header>

                </section>
                <section class="panel">


                    <div>
                        <!--左侧角色栏　-->

                        <div style="float: left ;margin-top: 10px;margin-left: 50px">
                            <button type="button" class="layui-btn layui-btn-fluid" style="height: 43px;"><h2>角色管理</h2>
                                <div>
                                    <input type="button" class="layui-btn " id="addDep"
                                           style="float: right;margin-top: -31px;" value="添加部门"/>
                                </div>
                            </button>

                            <div id="test9" class="demo-tree demo-tree-box"
                                 style="width: 500px; height: 500px; overflow: scroll;">

                            </div>
                        </div>
                        <div style="float: left;margin-top: 10px;margin-left: 90px;">
                            <button type="button" class="layui-btn layui-btn-fluid" style="height: 43px;"><h2>权限分配</h2>
                            </button>
                            <!--右侧权限设置栏-->
                            <div id="mydemo" class="demo-tree demo-tree-box"
                                 style="width: 500px; height: 500px; overflow: scroll;">

                            </div>
                        </div>


                        <input type="button" id="saveRole" class="layui-btn  layui-btn-lg "
                               style="float: right;margin-top: 10px;margin-right: 28px" value="保存"/>

                    </div>
                </section>
            </div>
            <!-- page end-->
        </section>
    </section>
    <!--main content end-->
</section>
<script src="js/jquery.js"></script>
<script>
    var data1;
    var data2;
    layui.use(['laydate', 'form', 'tree', 'jquery'], function () {
        var tree = layui.tree;

        //角色加载
        $.ajax({
            url: "yly-api/admin/queryAllPosition",
            type: "get",
            headers: {"token": localStorage.token, "url": window.location.pathname},
            success: function (data) {
                console.log(data);
                data1 = data.data;
                //开启节点操作图标
                var inst1 = tree.render({
                    elem: '#test9'
                    , data: data1
                    , showCheckbox: true
                    , id: "personTree"
                    , edit: ['add', 'update', 'del'] //操作节点的图标
                    ,oncheck: function(obj){
                        console.log(obj.data); //得到当前点击的节点数据
                        console.log(obj.checked); //得到当前节点的展开状态：open、close、normal
                        console.log(obj.elem); //得到当前节点元素

                        $.ajax({
                            url:"yly-api/admin/findFunctionByPost",
                            type:"post",
                            headers: {"token": localStorage.token, "url": window.location.pathname},
                            data:{"id":obj.data.id},
                            success: function (res) {
                                if (res.code == 200) {
                                    console.log(res.data);

                                    let data=[];
                                    $.each(res.data,function (index,value) {
                                        data[index]=value.id;
                                    })

                                    if (obj.checked==true){
                                        tree.setChecked('roleTree',data);
                                    }else {
                                        tree.reload('roleTree', {
                                            //新的参数
                                        });
                                    }

                                } else if (res.code == 500) {
                                    location.href = "500.html";
                                }
                            }, error: function (error) {
                                console.log(error.status);
                                if (error.status == 401) {
                                    layer.alert("权限不足，无法访问!");
                                    location.href = "login.html";
                                } else if (error.status == 403) {
                                    layer.alert("用户已过期，请重新登录！");
                                    localStorage.token = null;
                                    location.href = "login.html";
                                }
                            }
                        })


                    },
                    operate: function (obj) {
                        var type = obj.type; //得到操作类型：add、edit、del
                        var data = obj.data; //得到当前节点的数据
                        var elem = obj.elem; //得到当前节点元素

                        //Ajax 操作
                        var id = data.id; //得到节点索引
                        if (type === 'add') { //增加节点
                            let a = id.substring(id.lastIndexOf("-") + 1);
                            console.log(a);
                            $.ajax({
                                url: "yly-api/admin/addPosition",
                                type: "post",
                                data: {departmentId: a},
                                headers: {"token": localStorage.token, "url": window.location.pathname},
                                success: function (res) {
                                    if (res.code == 200) {
                                        if (res.data == true) {
                                            alert("添加成功！");
                                            location.href = "function.html";
                                        } else {
                                            layer.alert("添加失败,请联系管理员！");
                                        }
                                    } else if (res.code == 500) {
                                        location.href = "500.html";
                                    }
                                }, error: function (error) {
                                    console.log(error.status);
                                    if (error.status == 401) {
                                        layer.alert("权限不足，无法访问!");
                                        location.href = "login.html";
                                    } else if (error.status == 403) {
                                        layer.alert("用户已过期，请重新登录！");
                                        localStorage.token = null;
                                        location.href = "login.html";
                                    }
                                }
                            });
                            //返回 key 值
                            return id;
                        } else if (type === 'update') { //修改节点
                            console.log(elem.find('.layui-tree-txt').html()); //得到修改后的内容


                            let a = id.substring(id.lastIndexOf("-") + 1);
                            console.log(a);
                            //修改
                            $.ajax({
                                url: "yly-api/admin/updatePosition",
                                type: "post",
                                data: {id: id, post: elem.find('.layui-tree-txt').html()},
                                headers: {"token": localStorage.token, "url": window.location.pathname},
                                success: function (res) {
                                    if (res.code == 200) {
                                        if (res.data == true) {
                                            layer.alert("修改成功！");
                                            // location.href="function.html";
                                        } else {
                                            layer.alert("修改失败,请联系管理员！");
                                        }
                                    } else if (res.code == 500) {
                                        location.href = "500.html";
                                    }
                                }, error: function (error) {
                                    console.log(error.status);
                                    if (error.status == 401) {
                                        layer.alert("权限不足，无法访问!");
                                        location.href = "login.html";
                                    } else if (error.status == 403) {
                                        layer.alert("用户已过期，请重新登录！");
                                        localStorage.token = null;
                                        location.href = "login.html";
                                    }
                                }
                            });


                        } else if (type === 'del') { //删除节点

                            if (id.indexOf("-")!=-1){
                                let a = id.substring(id.lastIndexOf("-") + 1);
                                $.ajax({
                                    url: "yly-api/admin/deletePosition",
                                    data: {id: a},
                                    type: "post",
                                    headers: {"token": localStorage.token, "url": window.location.pathname},
                                    success: function (res) {
                                        if (res.code == 200) {
                                            if (res.data == true) {
                                                layer.alert("删除成功！");
                                            } else {
                                                layer.alert("删除失败,请联系管理员！");
                                            }
                                        } else if (res.code == 500) {
                                            location.href = "500.html";
                                        }
                                    }, error: function (error) {
                                        console.log(error.status);
                                        if (error.status == 401) {
                                            layer.alert("权限不足，无法访问!");
                                            location.href = "login.html";
                                        } else if (error.status == 403) {
                                            layer.alert("用户已过期，请重新登录！");
                                            localStorage.token = null;
                                            location.href = "login.html";
                                        }
                                    }
                                })
                            }else {
                                $.ajax({
                                    url: "yly-api/admin/deletePosition",
                                    data: {id: id},
                                    type: "post",
                                    headers: {"token": localStorage.token, "url": window.location.pathname},
                                    success: function (res) {
                                        if (res.code == 200) {
                                            if (res.data == true) {
                                                layer.alert("删除成功！");
                                            } else {
                                                alert("删除失败,请先删除该部门下的所有角色！");
                                                location.href="function.html";
                                            }
                                        } else if (res.code == 500) {
                                            location.href = "500.html";
                                        }
                                    }, error: function (error) {
                                        console.log(error.status);
                                        if (error.status == 401) {
                                            layer.alert("权限不足，无法访问!");
                                            location.href = "login.html";
                                        } else if (error.status == 403) {
                                            layer.alert("用户已过期，请重新登录！");
                                            localStorage.token = null;
                                            location.href = "login.html";
                                        }
                                    }
                                })
                            }

                        }
                    }
                });

                $(".layui-tree-pack").find(".layui-tree-btnGroup").find(".layui-icon-add-1").attr("style", "display:none");

            }, error: function (error) {
                console.log(error.status);
                if (error.status == 401) {
                    layer.alert("权限不足，无法访问!");
                    location.href = "login.html";
                } else if (error.status == 403) {
                    layer.alert("用户已过期，请重新登录！");
                    localStorage.token = null;
                    location.href = "login.html";
                }
            }
        });


        //权限加载
        $.ajax({
            url: "yly-api/admin/queryFunctionality",
            type: "get",
            headers: {"token": localStorage.token, "url": window.location.pathname},
            success: function (data) {
                console.log(data);
                data2 = data.data;

                var my2 = tree.render({
                    elem: '#mydemo'
                    , id: "roleTree"
                    , data: data2
                    , showCheckbox: true
                });
            }, error: function (error) {
                if (error.status == 401) {
                    layer.alert("权限不足，无法访问!");
                    location.href = "login.html";
                } else if (error.status == 403) {
                    layer.alert("用户已过期，请重新登录！");
                    localStorage.token = null;
                    location.href = "login.html";
                }
            }
        })
    });

    //添加部门
    $("#addDep").click(function () {
        layer.prompt({title: '请输入部门名称'}, function (val, index) {
            $.ajax({
                url: "/yly-api/admin/addDepartment",
                type: "post",
                headers: {"token": localStorage.token, "url": window.location.pathname},
                data: {departmentName: val},
                success: function (res) {
                    console.log(res);
                    if (res.code == 200) {
                        if (res.data == true) {
                            layer.msg('添加了' + val);
                            layer.close(index);
                            location.href = "function.html";
                        } else {
                            layer.alert("添加失败,请联系管理员！");
                        }
                    } else if (res.code == 500) {
                        location.href = "500.html";
                    }
                },
                error: function (error) {
                    if (error.status == 401) {
                        layer.alert("权限不足，无法访问!");
                        location.href = "login.html";
                    } else if (error.status == 403) {
                        layer.alert("用户已过期，请重新登录！");
                        localStorage.token = null;
                        location.href = "login.html";
                    }
                }
            })
        });
    });

    var $ = layui.jquery;
    var tree = layui.tree;



    /*保存按钮的点击事件*/
    $("#saveRole").click(function () {
        var checkedData = tree.getChecked('roleTree'); //获取选中节点的数据
        var personData = tree.getChecked("personTree");
        // layer.alert("角色" + JSON.stringify(personData) + "\n" + "权限：" + JSON.stringify(checkedData));

        $.ajax({
            type: "post",
            url: "/yly-api/admin/permissionAssignment",
            headers: {"token": localStorage.token, "url": window.location.pathname},
            data: {"post": JSON.stringify(personData), "fun": JSON.stringify(checkedData)},
            success: function (res) {
                console.log(res);
                if (res.code == 200) {
                    if (res.data == true) {
                        alert("授权成功！");
                        location.href = "function.html";
                    } else {
                        alert("修改失败,请联系管理员！");
                    }
                } else if (res.code == 500) {
                    location.href = "500.html";
                }
            },
            error: function (error) {
                if (error.status == 401) {
                    layer.alert("权限不足，无法访问!");
                    location.href = "login.html";
                } else if (error.status == 403) {
                    layer.alert("用户已过期，请重新登录！");
                    localStorage.token = null;
                    location.href = "login.html";
                }
            }
        })
    });


</script>


<script>var _hmt = _hmt || [];
(function () {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();</script>

<!-- js placed at the end of the document so the pages load faster -->

<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.scrollTo.min.js"></script>
<script src="js/jquery.nicescroll.js" type="text/javascript"></script>
<script type="text/javascript" src="assets/data-tables/DT_bootstrap.js"></script>

<!--common script for all pages-->
<script src="js/common-scripts.js"></script>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>
